Repository navigation
Increase expected size of output sample buffer, to correctly handle input termination. - #22
Open
arlofaria-cartesia wants to merge 2 commits into
Open
arlofaria-cartesia wants to merge 2 commits into
arlofaria-cartesia wants to merge 2 commits into
Conversation
This was referenced Nov 19, 2025
shauneccles
referenced
this pull request
in LedFx/samplerate-ledfx
Sep 27, 2026
… use Output size: a src_process() call can generate more than ceil(input_frames * ratio). With end_of_input the converter flushes the input it held back (about ratio x its filter half-length: 144 input frames for sinc_best, so ~37k output frames at ratio 256), and after a ratio change libsamplerate ramps from the previous ratio, which has no fixed bound. Today process() silently drops that output, and a fixed extra allowance (#22) only moves the failure. Resampler.process() now keeps calling src_process() with a larger buffer until libsamplerate stops short of filling it. resample() uses the same path, since src_simple() is src_new() + one src_process() + src_delete() and cannot continue. Callback: hold the GIL for the whole of the_callback_func, since the buffer_info destructor releases a Python buffer, and store any exception (ours or one raised by the Python callback) instead of letting it unwind through libsamplerate's C frames; read() re-raises it. Also run the asyncio tests in CI (pytest-asyncio). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem: when using the full API (
.process()method in this wrapper), the number of output samples is incorrect for a chunked input at the end of a stream. This is because the calculation ofnew_sizeassumes that the number of output samples that will be generated will be at most the number of input samples times the resampling ratio. While that's generally true for chunks processed mid-stream (as well as at the start of a stream), when the input is terminated it becomes possible for the output samples to exceed this expected length because the resampler's buffer is being flushed of any remaining data.Solution: an ideal solution would be to determine the actual number of samples that will be flushed on an input-terminating call. This is complicated to do, for various reasons related to issues such as libsndfile/libsamplerate#6
As a hacky workaround, we can figure that adding a "fudge factor" of 10,000 samples ought to be sufficient. In case that's not enough, a
RuntimeErrorwill be raised.Testing: Here's a modified version of the example code on the
README... perhaps this chunked usage should be included in the documentation?